package org.ufinity.hsms.module.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import org.ufinity.hsms.common.utils.SystemUtils;
import org.ufinity.hsms.module.TabbarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class HomeActivity extends BaseActivity implements OnClickListener, OnItemClickListener{
	public static final String TAG = "HomeActivity";
	private HSMApplication application;
	TextView txt_title;
	
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	GridView gridview;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.home);
		
		showInfo();
	}

	private void showInfo() {
		
		gridview = (GridView) findViewById(R.id.gridview);
		
		list = addItem();
		SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.home_menu_list_item, new String[] { "img", "name" }, new int[] { R.id.imageMenu, R.id.textMenu });
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(this);

		DisplayMetrics dd = getApplicationContext().getResources().getDisplayMetrics();
		if (dd.heightPixels > 480) {
			float height = ((dd.heightPixels - 390) * 160 / dd.ydpi - 200) / 4;
			height = height / 160 * dd.ydpi;
			int h = (int) height;
			gridview.setVerticalSpacing(h);
		}
	}

	private List<Map<String, Object>> addItem() {
		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < mImageIds.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("img", mImageIds[i]);
			map.put("name", getResources().getString(mNameIds[i]));
			items.add(map);
		}

		return items;
	}

	private Integer[] mImageIds = {
		R.drawable.home_icon,
		R.drawable.dashboard_icon,
		R.drawable.alarm_icon,
		R.drawable.monitordata_icon,
		R.drawable.bussymonitor_icon,
		R.drawable.setting_icon,};

	private Integer[] mNameIds = { R.string.home,
			R.string.master_control_panel, R.string.alarm,
			R.string.monitor_data, R.string.bussy_monitor, R.string.setting };

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == 0) {
			// home
		}
		else if (position == 1) {
			// 总控面板
			TabbarActivity.group.check(R.id.tab_pannel);
		}
		else if (position == 2) {
			// 当前报警
			TabbarActivity.group.check(R.id.tab_alarm);
		}
		else if (position == 3) {
			// 监控数据
			TabbarActivity.group.check(R.id.tab_device);
		}
		else if (position == 4) {
			// 业务监控
			TabbarActivity.group.check(R.id.tab_busy);
		}
		else if (position == 5) {
			// 设置
			TabbarActivity.group.check(R.id.tab_settings);
		}
		
	}
	
	@Override
	public void onClick(View v) {
		
	}

	/**
	 * 锁back键 控制提示退出
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			SystemUtils.openOptionsDialog(getParent(), application);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		return super.dispatchKeyEvent(event);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	
}
